﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Edge.Business.Domain.Application;
using Edge.Common.ComponentModel.Parameters;
using Edge.Common.Services;
using Shop.Business.Domain.Customers;
using Shop.Business.Domain.Orders;
using Shop.Web.AppCode;

namespace Shop.Web
{
    public partial class CustomerCabinet : ShopBasePage
    {
        [QueryParameter("id", IsRequired = false)]
        public int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {

            SetTitle("Личный кабинет"); 
            if (Locator.GetService<IContextObjects>().Get<CustomerHolder>() == null)
            {
                AccessDeniedMessage();
            }
            else
            {
                WebParameterAttribute.SetValues(this, Request);
                Customer currentCustomer = Locator.GetService<IContextObjects>().Get<CustomerHolder>().Object;
                Customer customer = currentCustomer;
                if (id > 0 && currentCustomer.Role != CustomerRole.Buyer)
                    customer = Locator.GetService<ICustomerManager>().Load(id);
                ltName.Text = customer.FullName;
                ltDiscount.Visible = customer.Discount > 0;
                if (ltDiscount.Visible)
                {
                    ltDiscount.Text = "Скидка: " + customer.Discount.ToString() + "%";
                }
                ltMoney.Visible = customer.Credits > 0;
                if (ltMoney.Visible)
                {
                    ltMoney.Text = "Баланс: " + customer.Credits.ToString("C");
                }
                ddlCustomers.Visible = currentCustomer.Role != CustomerRole.Buyer;
                btnChangeCustomer.Visible = currentCustomer.Role != CustomerRole.Buyer;
                if (ddlCustomers.Visible && !IsPostBack)
                {
                    ddlCustomers.DataSource = Locator.GetService<ICustomerManager>().FindAll().ToList().OrderBy(c => c.LastName);
                    ddlCustomers.DataTextField = "FullNameDdl";
                    ddlCustomers.DataValueField = "Id";
                    ddlCustomers.SelectedValue = customer.Id.ToString();
                    
                    ddlCustomers.DataBind();
                    ddlCustomers.Items.Add(new ListItem("все пользователи", "-99"));
                }
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected void btnChangeCustomer_Click(object sender, EventArgs e)
        {
            Response.Redirect("Customer.aspx?id=" + ddlCustomers.SelectedValue);
        }
    }
}
